// ==UserScript==
// @name         爱玩物扩展
// @version      1.1
// @description  爱玩物扩展，添加两个按钮，将所有异常任务开始，批量删除完成任务
// @author       zqbinary
// @match        http://192.168.1.4:8283/owdl/index.html
// @match        http://zqbinary.icu:8283/owdl/index.html
// @match        https://act-vip-ssl.xunlei.com/remote/*
// @run-at         document-end
// ==/UserScript==

var isOwdl = /http:\/\/.*\/owdl\/index.html/.test(location.href)
if (isOwdl) {
    document.getElementById('iframe').onload = function (o) {
        var iframe = document.getElementById('iframe')
        var src = iframe.src;
        window.location.href = src;
    }
}
var isThunder = /act-vip-ssl.xunlei.com/.test(location.host)
var bottomStyle = `
                border: 1px solid green;
                padding: 2px 5px;
                margin-left: 11px;
                line-height: 34px;
                color: #000;
                vertical-align: top;
                cursor: pointer;
                border-radius: 3px;
            `;
if (isThunder) {
    //onload body 的时候元素尚未出来
    setTimeout(function () {
        var mainBar = document.getElementsByClassName("main-addbar")[0]
        var allStart = document.createElement("span");
        allStart.innerText = '开始';
        allStart.onclick = function () {
            var doms = document.getElementsByClassName("item-down");
            var len = doms.length || 0;
            var processText, itemDom, process, reason, reasonCode;
            for (var i = 0; i < len; i++) {
                itemDom = doms[i].parentElement.parentElement;
                processText = itemDom.getElementsByClassName('item-state')[0].getElementsByClassName('item-size')[0].innerText
                reason = itemDom.getElementsByClassName("item-speed")[0].innerText
                reasonCode = reason.slice(reason.indexOf('[') + 1, reason.indexOf(']'))
                process = processText.substr(0, processText.indexOf(' '));
                if (!(0 == process && '111154' == reasonCode)) {
                    doms[i].click();
                }

            }
        }
        allStart.style = bottomStyle;
        mainBar.appendChild(allStart);

        var allDel = document.createElement("span");
        allDel.innerText = '清空';
        allDel.onclick = function () {
            var doms = document.getElementsByClassName("item-del");
            var len = doms.length || 0;
            var itemDom;
            for (var i = 0; i < len; i++) {
                doms[i].click();
                //setTimeout(function(){
                document.getElementsByClassName('confirm')[0].click();
                //},100)

            }
            location.reload();
        }
        allDel.style = bottomStyle;
        mainBar.appendChild(allDel);

    }, 1000);
}

